<template>
	<view>
		<!-- <u-navbar title="我的证书"></u-navbar> -->
		<view style="width: 100%;display: flex;flex-direction: column;padding: 24rpx;box-sizing: border-box;background: #FFFFFF;margin-top: 24rpx;">
			<view class="my-center-menu" style="display: flex;flex-direction: column;justify-content: center;background: #FFFFFF;margin-top: 36rpx;padding: 0 24rpx;box-sizing: border-box;font-size: 32rpx;">
				<view style="width: 100%;border-bottom: 1px solid #dedede; height: 112rpx;display: flex;align-items: center;" v-for="(item,index) in certificateList" :key="index">
					<view style="flex: 1;display: flex;flex-direction: column;"  >
						<view>{{item.qualificationName}}</view>
						<view>{{item.obtainTime}}</view>
					</view>
					<view style="width: 220rpx; height: 80rpx; text-align: center; line-height: 80rpx; border-radius: 80rpx; color: #FFFFFF; background: linear-gradient(109.9831065219deg, rgba(246, 158, 151, 1) 0%, rgba(234, 101, 94, 1) 100%);">
						查看证书
					</view>
				</view>
			</view>
		</view>
		<mosowe-canvas-image
			ref="refImgComponent"
			:lists="lists"
			@canvasImage="canvasImage"
			width="750"
			height="1125"
		/>
		<view v-model="popupShow" style="position: fixed;top:0;left:0; width: 100%;height: 100vh; display: flex;flex-direction: column;justify-content: center;align-items: center; background: rgba(0,0,0,0.5);">
			<view  style="width: 600rpx;height: 70vh;position: relative;">
				<image src="../../static/cert/cert.jpg" style="width: 100%;height: 100%;"></image>
				<view style="position: absolute;width: 100%;padding: 30rpx;box-sizing: border-box; top:380rpx;left:60rpx;">
					<view style="font-weight: bold;">{{userName || '测试'}}</view>
					<view style="margin-top:20rpx;">恭喜您！</view>
					<view style="margin-top:30rpx;font-weight: bold;">已完成{{certInfo.bodyText}}</view>
					<view style="margin-top:20rpx;">考试合格，特发此证。</view>
					
					
				</view>
				<view style="position: absolute;bottom: 50px;text-align: right;width: 100%;padding-right: 60rpx;">
					<text>{{certInfo.certificationAuthority || '安利'}}</text><br/>
					<text>{{`${new Date().getFullYear()}.${new Date().getMonth()+1>=10?(new Date().getMonth()+1): '0'+(new Date().getMonth()+1)}.${new Date().getDate()>=10?new Date().getDate():'0'+new Date().getDate()}`}}</text>
				</view>
				
			</view>
			
				
			<button style="background: linear-gradient(141deg,#9fb8ad 0%, #da155d 51%,#a8038e 100%);color:#fff; width: 300rpx;margin-top:20rpx;border-radius: 100rpx;"
				@click="onSaveImage">保存图片</button>
		</view>
	</view>
</template>

<script>
	import mosoweCanvasImage from "../../components/mosowe-canvas-image/mosowe-canvas-image.vue"
	export default {
		components:{
			mosoweCanvasImage
		},
		data() {
			return {
				certificateList:[],
				certInfo:{},
				popupShow:true,
				userName:'',
				lists:[
					{
						type: 'image',
						content: '../../static/cert/cert.jpg', 
						width: '750', 
						height: '1125', 
						x: 0, 
						y: 0, 
					},
					{
						type: 'text',
						content: '测试', 
						x: 110, 
						y: 530, 
						color: '#000', 
						size: 32, 
					},
					{
						type: 'text',
						content: '恭喜您！', 
						x: 110, 
						y: 590, 
						color: '#000', 
						size: 32, 
					},
					{
						type: 'text',
						content: '您在本次培训学习中表现优异', 
						x: 110, 
						y: 650, 
						color: '#000', 
						size: 32, 
						// maxWidth: 1000, 
						align: 'left', 
					},
					{
						type: 'text',
						content: '考试合格，特发此证。', 
						x: 110, 
						y: 710, 
						color: '#000', 
						size: 32, 
						// maxWidth: 1000, 
						align: 'left', 
					},
					{
						type: 'text',
						content: '点是架', 
						x: 660, 
						y: 900, 
						color: '#000', 
						size: 32, 
						// width: 1000, 
						align: 'right', 
					},
					{
						type: 'text',
						content: `${new Date().getFullYear()}.${new Date().getMonth()+1>=10?(new Date().getMonth()+1): '0'+(new Date().getMonth()+1)}.${new Date().getDate()>=10?new Date().getDate():'0'+new Date().getDate()}`, 
						x: 660, 
						y: 960, 
						color: '#000', 
						size: 32, 
						// width: 1000, 
						align: 'right', 
					},
				]
			}
		},
		onLoad(option) {
			try {
				let obj = uni.getStorageSync('mlgjFontSize')
				if(obj){
					this.userName = obj.nickName
				}
			} catch (e){
				console.log(e)
			}
			this.initCertificate()
			this.viewCertificate()
		},
		mounted(){
			
		},
		methods: {
			initCertificate(){
				this.$http.getMyQualificationInfo().then(res=>{
					this.certificateList = res.data
				})
			},
			viewCertificate(){
				this.$http.getMyQualificationById('205ff24e2522faa0c198d6a950da631d').then(res=>{
					console.log(res.data)
					this.certInfo = res.data
				})
			},
			
			canvasImage(e){
				uni.saveImageToPhotosAlbum({
					filePath: e,
					success:() => {
						uni.hideLoading()
						uni.showToast({
							title:'图片已保存至相册'
						})
					},
					fail: () => {
						uni.hideLoading()
						uni.showToast({
							title:'图片保存失败'
						})
					}
				})
			},
			onSaveImage(){
				// 绑定数据
				this.lists[1].content = this.userName || '测试11'
				this.lists[3].content = this.certInfo.bodyText || '测试证书主体，内容不要太长哦'
				this.lists[5].content = this.certInfo.certificationAuthority || '测试机构名称'
				this.lists[6].content = `${new Date().getFullYear()}.${new Date().getMonth()+1>=10?(new Date().getMonth()+1): '0'+(new Date().getMonth()+1)}.${new Date().getDate()>=10?new Date().getDate():'0'+new Date().getDate()}`
				this.lists = this.lists.slice()
				this.$refs.refImgComponent.createCanvas()
			}
		}
	}
</script>

<style>

</style>
